package com.sgb.kjwl.viewmodel.manage

import androidx.lifecycle.MutableLiveData
import com.sgb.kjwl.api.APIManager
import com.sgb.kjwl.model.entity.manage.special.SpecialMemberEntity
import com.swgk.core.base.BaseViewModel
import com.swgk.core.base.api.ICallBack
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.base.model.entity.ListResponse

class MemberUnitViewModel : BaseViewModel() {

    private var page = 1;
    private val limit = 10

    private val map = HashMap<String, Any>()

    //成员单位
    val mMemberUnitList = MutableLiveData<List<SpecialMemberEntity>>()

    //解除关系
    val mPosition = MutableLiveData<Int>()


    /**
     * 成员单位 列表
     * companyName 搜索公司名称
     */
    fun getMemberUnitList(companyName : String, isLoadMore : Boolean){
        page = if (isLoadMore) page + 1 else 1
        map["page"] = page
        map["limit"] = limit
        map["companyName"] = companyName
        APIManager.getInstance().websiteInfoAPI.getMemberUnitList(map).enqueue(object : ICallBack<BaseEntity<ListResponse<List<SpecialMemberEntity>>>>(true, true){
            override fun onSuccess(data: BaseEntity<ListResponse<List<SpecialMemberEntity>>>?) {
                if (data != null && data.data != null && data.data.list != null) {
                    mMemberUnitList.postValue(data.data.list)
                }
            }

        })
    }

    /**
     * 成员单位 解除关系
     * id
     */
    fun relieveMember(id : Int, position : Int){
        APIManager.getInstance().websiteInfoAPI.relieveMemberUnit(id).enqueue(object : ICallBack<BaseEntity<*>>(true, true){
            override fun onSuccess(data: BaseEntity<*>?) {
                if (data != null){
                    mPosition.postValue(position)
                }
            }
        })
    }
}